package edu.zjut.hjg.saas.biz.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import edu.zjut.hjg.saas.biz.dao.KindergartenDao;
import edu.zjut.hjg.saas.biz.dao.PriceTypeDao;
import edu.zjut.hjg.saas.biz.dao.SubscribeDao;
import edu.zjut.hjg.saas.biz.dao.UserDao;
import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.model.PriceType;
import edu.zjut.hjg.saas.biz.model.Subscribe;
import edu.zjut.hjg.saas.biz.model.Tenant;
import edu.zjut.hjg.saas.biz.model.User;
import edu.zjut.hjg.saas.biz.service.SubscribeService;
import edu.zjut.hjg.saas.biz.support.AppStatus;
import edu.zjut.hjg.saas.biz.support.ChargeStatus;
import edu.zjut.hjg.saas.biz.support.Paginated;
import edu.zjut.hjg.saas.biz.support.RoleName;
import edu.zjut.hjg.saas.biz.util.PriceTypeUtil;

public class SubscribeServiceImpl implements SubscribeService {

    private static final Log log = LogFactory.getLog(SubscribeServiceImpl.class);

    private SubscribeDao     subscribeDao;
    private KindergartenDao  kindergartenDao;
    private PriceTypeDao     priceTypeDao;
    private UserDao          userDao;

    public void setSubscribeDao(SubscribeDao subscribeDao) {
        this.subscribeDao = subscribeDao;
    }

    public void setKindergartenDao(KindergartenDao kindergartenDao) {
        this.kindergartenDao = kindergartenDao;
    }

    public void setPriceTypeDao(PriceTypeDao priceTypeDao) {
        this.priceTypeDao = priceTypeDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    @Transactional
    public void subscribeApp(int priceTypeId, int kindergartenId, int leaseTerm) {
        Kindergarten kindergarten = kindergartenDao.load(kindergartenId);
        PriceType priceType = priceTypeDao.load(priceTypeId);
        if (kindergarten == null || priceType == null) {
            String message = "no kindergarten or priceType found: kindergartenId=" + kindergartenId
                    + ", priceTypeId=" + priceTypeId;
            log.error(message);
            throw new RuntimeException(message);
        }
        // 启用状态和租期
        kindergarten.setStatus(AppStatus.ENABLED);
        kindergarten.setStartDate(new Date());
        kindergarten.setEndDate(PriceTypeUtil.getEndDate(priceType, leaseTerm, true));
        kindergartenDao.update(kindergarten);

        // 创建该网站的一个用户
        User user = new User();
        Tenant tenant = kindergarten.getTenant();
        user.setKindergarten(kindergarten);
        user.setLoginId(tenant.getLoginId());
        user.setName(tenant.getName());
        user.setPassword(tenant.getPassword());
        user.setRoleName(RoleName.ADMIN);
        userDao.create(user);

        // 订购关系
        Subscribe subscribe = new Subscribe();
        subscribe.setPriceType(priceType);
        subscribe.setKindergarten(kindergarten);
        // 免费试用期
        subscribe.setChargeStatus(ChargeStatus.FREE_TRIAL);
        subscribeDao.create(subscribe);
    }

    @Override
    public Subscribe findByKindergartenId(int kindergartenId) {
        return subscribeDao.findByKindergartenId(kindergartenId);
    }

    @Override
    public Paginated<Subscribe> listByAppStatus(AppStatus status, int pageSize, int page) {
        return subscribeDao.listByAppStatus(status, pageSize, page);
    }
}
